﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.ICM;
using System;
using System.Linq;
using System.Web.Mvc;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class WarehouseController : Controller
    {
        private GERPContext db = new GERPContext();

        public ActionResult Index()
        {
            var service = new WarehouseService.WarehouseServiceClient();

            return View(service.SelectWareHouseOfInternalJobSite(new Models.ICM.Warehouse { RecordStatus = "!D" }, new[] {"Scope", "Jobsite"}));   
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Warehouse m)
        {
            if (ModelState.IsValid)
            {
                var service = new WarehouseService.WarehouseServiceClient();
                int id = service.Insert(m, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Details", new { id = id });
            }
            return View(m);
        }

        public ActionResult CreatePermit(int WhId)
        {
            var service = new WarehouseService.WarehouseServiceClient();
            var modelWarehouse = service.Select(new Models.ICM.Warehouse { WhId = WhId }, null).FirstOrDefault();
            var model = new WarehousePermit();
            model.WhId = modelWarehouse.WhId;

            return PartialView(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreatePermit(WarehousePermit model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new WarehouseService.WarehouseServiceClient();
                    var id = service.InsertPermit(model, Class.GimsMembership.CurrentEmpId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }

            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
                //throw ex;
            }

            return Json(new { result = false, message = KPIS.GERP.Models.Class.ValidateText.RequiredField, alertClass = "" });
        }

        public ActionResult Edit(int id)
        {
            var service = new WarehouseService.WarehouseServiceClient();
            var model = service.Select(new Models.ICM.Warehouse { WhId = id }, null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, Warehouse model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.UpdatedWhen = DateTime.Now;

                    var service = new WarehouseService.WarehouseServiceClient();
                    service.Update(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View();
        }

        public ActionResult EditPermit(int id)
        {
            var service = new WarehouseService.WarehouseServiceClient();
            var model = service.SelectWarehousePermit(new Models.ICM.WarehousePermit { WarehousePermitId = id }, null).FirstOrDefault();

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditPermit(int id, WarehousePermit model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new WarehouseService.WarehouseServiceClient();
                    service.UpdatePermit(model, Class.GimsMembership.CurrentEmpId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = KPIS.GERP.Models.Class.ValidateText.RequiredField, alertClass = "" });
        }

        public ActionResult Details(int id)
        {
            var service = new WarehouseService.WarehouseServiceClient();
            var model = service.Select(new Models.ICM.Warehouse { WhId = id}, new[] { "Jobsite", "Scope" }).FirstOrDefault();

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new WarehouseService.WarehouseServiceClient();
                service.Delete(id, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new WarehouseService.WarehouseServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.Delete(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        public ActionResult DeletePermit(int WarehousePermitId)
        {
            try
            {
                var service = new WarehouseService.WarehouseServiceClient();
                var WhId = service.SelectWarehousePermit(new Models.ICM.WarehousePermit { WarehousePermitId = WarehousePermitId }, null).FirstOrDefault().WhId;
                service.DeletePermit(WarehousePermitId, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Details", new { id = WhId });
            }
            catch
            {
                return View("Details");
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAllPermit(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new WarehouseService.WarehouseServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeletePermit(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }
    }
}
